#! /usr/bin/python
# -*- coding: cp1250 -*-
#import sys; sys.path.append(r'C:/xxx/yyy')

import glob, re, string, sys, os, shutil, urllib2

import pepe.p_txt.bigtxt as bt
import pepe.p_filesystem.basic as pfb

def backup_gtw_file():
    """ """
    #C:\AFT\mojo\data-twhtml
    ## copy last aftxxxxxxxxxx.html
    #FROM_FILE = sorted(pfb.fslist('W:/aft/docs/tw-related/', patterns='aft*.html;', recurse=False))[-2]
    FROM_FILE = sorted(pfb.fslist('C:/AFT/root/TiddlyWiki/', patterns='afttw*.html;', recurse=False))[-2]
    TO_DIR = 'C:/AFT/bckp/'
    print FROM_FILE, 'copied to',  TO_DIR
    shutil.copy(FROM_FILE, TO_DIR)

    '''
    ## copy aft.html
    FROM_FILE = 'W:/aft/docs/tw-related/afttw.html'
    TO_DIR = 'C:/AFT/bckp/'
    print FROM_FILE, 'copied to',  TO_DIR
    shutil.copy(FROM_FILE, TO_DIR)
    '''

    '''
    # copy/backup vn* files
    FROM_FILES = pfb.fslist(FROM_DIR, patterns='vn*.html;', recurse=False)

    for ifile in FROM_FILES:
        print ifile, 'copied to',  TO_DIR
        shutil.copy(ifile, TO_DIR)
    '''

def backup_private_tw_file():
    """ """
    FROM_DIR = 'C:/AFT/root/TiddlyWiki/'
    TO_DIR = 'C:/AFT/bckp/'

    # copy/backup files
    FROM_FILE = sorted(pfb.fslist(FROM_DIR, patterns='tw*.html;', recurse=False))[-2]
    #print '\n\n', FROM_FILE, '\n\n'
    print FROM_FILE, 'copied to',  TO_DIR
    shutil.copy(FROM_FILE, TO_DIR)

def backupGDiskAftTw():
    ''' '''
    try:
        FROM_DIR = 'C:/AFT/root/TiddlyWiki/'
        FROM_FILE = sorted(pfb.fslist(FROM_DIR, patterns='afttw*.html;', recurse=False))[-2]
        #print '\n\n', FROM_FILE, '\n\n'
        shutil.copy(FROM_FILE, 'G:/AFT/Forensic-Aft-ISACOPYCHANGESARELOST.html')
        #shutil.copy(FROM_FILE, 'W:/aft/docs/Forensic-Aft-ISACOPYCHANGESARELOST.html')
        return True
    except:
        return False

def isWebOnline(resource):
    """ check whether web is available """
    #print isOnline(resource='http://www.google.com')
    try:
        response = urllib2.urlopen(resource)
    except:
        return False
    else:
        return True

if __name__ == "__main__":
    """ """
    if os.path.exists(r'g:/aft/Forensic-Aft-ISACOPYCHANGESARELOST.html'):
        print 'Copied to G:/AFT/Forensic-Aft-ISACOPYCHANGESARELOST.html : ', backupGDiskAftTw()
    backup_gtw_file()
    backup_private_tw_file()
    os.system('pause')

    #FROM_DIR = 'C:/AFT/root/TiddlyWiki/'
    #print sorted(pfb.fslist(FROM_DIR, patterns='tw*.html;', recurse=False))
